package com.phuc.game.andPuzzle;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;

import com.google.ads.AdView;
import com.phuc.game.andPuzzle.common.Utils;
import com.phuc.game.andPuzzle.engine.GamePlay;
import com.phuc.game.andPuzzle.engine.InstantEngine;
import com.phuc.game.andPuzzle.sprite.Puzzle;

public class PlayingActivity extends BaseGameActivity implements IOnSceneTouchListener {

	public static int SCENE_WIDTH = 480;
	public static final int SCENE_HEIGH = 700;
	public static final int CAMERA_WIDTH = 480;
	public static final int CAMERA_HEIGHT = 700;
	
	private Engine mEngine;
	private Scene mScene;
	GamePlay game;
	private static PlayingActivity playingActivity;
	
	Vibrator vibrator;
	private AdView adView;
	String level;
	private int touchIndex;
	boolean gameIsStop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		touchIndex = 0;
		gameIsStop = false;
		playingActivity = this;
		this.level = getIntent().getExtras().getString("level");
		Log.d("phucphuc", "phucphuc level " + level);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public Engine onLoadEngine() {
		InstantEngine ie = new InstantEngine(SCENE_WIDTH, SCENE_HEIGH,
				CAMERA_WIDTH, CAMERA_HEIGHT, true);
		mEngine = ie.getEngine();
		return mEngine;
	}

	@Override
	public void onLoadResources() {
		GamePlay.level = level;
		GamePlay.initTextures(mEngine, this);
	}

	@Override
	public Scene onLoadScene() {
		mEngine.registerUpdateHandler(new FPSLogger());
		game = new GamePlay(level);
		
		mScene = game.getWelcomeScene();
		mScene.setOnSceneTouchListener(this);
		return mScene;
	}

	@Override
	public void onLoadComplete() { }

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		touchIndex++;
		if (touchIndex == 10) {
//			adView.setVisibility(View.VISIBLE);
		} else if (touchIndex == 20) {
//			adView.setVisibility(View.INVISIBLE);
			touchIndex = 0;
		}
		if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
			for (Puzzle puzzle : game.puzzles) {
				if (puzzle.contains(pSceneTouchEvent.getX(), pSceneTouchEvent.getY())) {
					puzzle.moveToBlankCell();
				}
			}
		}
		game.checkEndGame();
		return false;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		playingActivity = null;
	}
	
	public void finishGame(int point) {
		gameIsStop = true;
		if (point < 1) {
			finish();
			return;
		}
		
		setContentView(R.layout.result);
		ImageView img = (ImageView)findViewById(R.id.imgView);
		ImageView imgPoint = (ImageView)findViewById(R.id.point);
		
		Utils.loadImageAssets(this, img, level);
		if (point == 1) {
			imgPoint.setImageResource(R.drawable.star_1);
		} else if (point == 2) {
			imgPoint.setImageResource(R.drawable.star_2);
		} else if (point == 3) {
			imgPoint.setImageResource(R.drawable.star_3);
		}
		
		int intLevel = Integer.parseInt(level);
		if (intLevel >= MainActivity.points.length) {
			int [] point2 = new int[MainActivity.points.length + 1];
			for (int i = 0; i < MainActivity.points.length; i++) {
				point2[i] = MainActivity.points[i];
			}
			point2[point2.length - 1] = point;
			MainActivity.points = point2;
			MainActivity.hasChanged = true;
		} else {
			if (point > MainActivity.points[intLevel]) {
				MainActivity.points[intLevel] = point;
				MainActivity.hasChanged = true;
			}
		}
		
	}
	
	public static PlayingActivity getInstant() {
		return playingActivity;
	}
	
	@Override
	public void onBackPressed() {
		if (gameIsStop) {
			super.onBackPressed();
		} else {
			Intent intent = new Intent(this, PauseActivity.class);
			startActivityForResult(intent, 1);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 10000) {
			game.endGame(false);
		}
	}
	
//	@Override
//	protected void onSetContentView() {
//		final FrameLayout frameLayout = new FrameLayout(this);
//	    final FrameLayout.LayoutParams frameLayoutLayoutParams =
//	    		new FrameLayout.LayoutParams(
//	    				FrameLayout.LayoutParams.FILL_PARENT,
//	    				FrameLayout.LayoutParams.FILL_PARENT);
//
//	    this.mRenderSurfaceView = new RenderSurfaceView(this);
//	    mRenderSurfaceView.setRenderer(mEngine);
//	    final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//	    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
//
//	    // Add view to scene
//	    LayoutInflater inflater = LayoutInflater.from(this);
//	    View v = inflater.inflate(R.layout.main, null);
//	    
//	    // Load Ads
//	    adView = (AdView) v.findViewById(R.id.adView);
//	    new Thread() {
//        	public void run() {
//        		adView.loadAd(new AdRequest());
//        	}
//        }.start();
//        adView.setVisibility(View.INVISIBLE);
//	    
//	    frameLayout.addView(v);
//	    
//	    //Create any other views you want here, and add them to the frameLayout.
//
//	    this.setContentView(frameLayout, frameLayoutLayoutParams);
//	}
}
